using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Snackbar Animator", 100)]
	public class SnackbarAnimator : ToastAnimator
	{
		[SerializeField]
		private Button m_ActionButton;

		[SerializeField]
		private Text m_ActionText;

		private Action m_OnActionButtonClicked;

		public void Show(Snackbar snackbar)
		{
			m_OnActionButtonClicked = snackbar.onActionButtonClicked;
			if (m_ActionButton != null)
			{
				m_ActionButton.gameObject.SetActive(!string.IsNullOrEmpty(snackbar.actionName));
				m_ActionText.text = snackbar.actionName.ToUpper();
			}
			Show((Toast)snackbar);
			StartCoroutine(Setup());
		}

		public void OnActionButtonClicked()
		{
			if (m_OnActionButtonClicked != null)
			{
				m_OnActionButtonClicked();
			}
			Vector3 position = m_RectTransform.position;
			m_CurrentPosition = position.y;
			m_State = 2;
			m_AnimStartTime = Time.realtimeSinceStartup;
		}

		protected override void OnAnimDone()
		{
			if (!SnackbarManager.OnSnackbarCompleted())
			{
				UnityEngine.Object.Destroy(base.gameObject);
			}
		}

		private IEnumerator Setup()
		{
			yield return new WaitForEndOfFrame();
			LayoutElement layoutElement = m_Text.GetComponent<LayoutElement>();
			float buttonWidth = m_ActionButton.GetComponent<MaterialButton>().preferredWidth;
			HorizontalLayoutGroup horizontalLayoutGroup = GetComponent<HorizontalLayoutGroup>();
			float otherWidth = buttonWidth + (float)horizontalLayoutGroup.padding.left + horizontalLayoutGroup.spacing;
			if (Screen.height > Screen.width)
			{
				Vector2 properSize = m_RectTransform.GetProperSize();
				float y = properSize.y;
				GetComponent<ContentSizeFitter>().horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
				m_RectTransform.sizeDelta = new Vector2(Screen.width, y);
				layoutElement.minWidth = (float)Screen.width - otherWidth - 4f;
				m_PanelImage.sprite = null;
				m_MaterialMovableFab = UnityEngine.Object.FindObjectOfType<MaterialMovableFab>();
				if (m_MaterialMovableFab != null)
				{
					m_FabRectTransform = m_MaterialMovableFab.GetComponent<RectTransform>();
					Vector3 position = m_FabRectTransform.position;
					m_FabStartPos = position.y;
					m_MoveFab = true;
				}
				else
				{
					m_FabRectTransform = null;
					m_MoveFab = false;
				}
			}
			else
			{
				layoutElement.minWidth = 288f - otherWidth;
				layoutElement.preferredWidth = -1f;
				LayoutRebuilder.MarkLayoutForRebuild(m_RectTransform);
				Vector2 properSize2 = m_RectTransform.GetProperSize();
				if (properSize2.x > 568f)
				{
					layoutElement.preferredWidth = 568f;
				}
			}
			ref Vector2 offPos = ref m_OffPos;
			Vector2 properSize3 = m_RectTransform.GetProperSize();
			offPos.y = (0f - properSize3.y) * 1.05f;
			m_RectTransform.position = m_OffPos;
			Vector3 position2 = m_RectTransform.position;
			m_CurrentPosition = position2.y;
			GetComponent<CanvasGroup>().alpha = 1f;
			m_OnPos.y = 0f;
			m_OffAlpha = 1f;
		}
	}
}
